import * as dotenv from 'dotenv';
import { EnvConfig } from './types';

dotenv.config();

const getConfig = (): EnvConfig => {
  if (!process.env.DASHSCOPE_API_KEY) {
    throw new Error('DASHSCOPE_API_KEY 未在环境变量中设置');
  }

  return {
    DASHSCOPE_API_KEY: process.env.DASHSCOPE_API_KEY,
    OPENAI_BASE_URL: process.env.OPENAI_BASE_URL || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
    MODEL: process.env.MODEL || 'deepseek-v3',
    MAX_HISTORY: parseInt(process.env.MAX_HISTORY || '100'),
    LOG_DIR: process.env.LOG_DIR || './logs',
    LOG_FORMAT: process.env.LOG_FORMAT || 'md',
    // 持久化相关
    CHAT_HISTORY_DIR: process.env.CHAT_HISTORY_DIR || './chat_records',
    MAX_HISTORY_BACKUPS: parseInt(process.env.MAX_HISTORY_BACKUPS || '3'),
    SESSION_TIMEOUT: parseInt(process.env.SESSION_TIMEOUT || '3600')
  };
};

export const config = getConfig();
